package cn.fly.Util.thread;

import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.*;

/**
 * @author mifei
 * @create 2018年05月03日
 **/
public class ThreadPoolExecutorUtil {

    private static int corePoolSize ;
    private static int maximumPoolSize ;
    private static long keepAliveTime ;
    private static int queueSize ;

    private static ThreadPoolExecutor executor = null;
    static {
        corePoolSize = 5;
        maximumPoolSize = 10;
        keepAliveTime = 200;
        queueSize = 5;
    }
    public static ThreadPoolExecutor getExecutor() {
        if (executor == null) {
            BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(queueSize);
            executor = new ThreadPoolExecutor(corePoolSize,
                    maximumPoolSize,
                    keepAliveTime,
                    TimeUnit.MILLISECONDS,
                    workQueue,
                    Executors.defaultThreadFactory(),
                    new RejectedExecutionHandler() {
                        @Override
                        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                            if (r instanceof ThreadMessage) {
                                ThreadMessage myTmask = (ThreadMessage) r;
                                //直接打印
                                System.out.println("recject===" + myTmask.getMessage());
                            }
                        }
                    }
            );
        }
        return executor;
    }


    public void setQueueSize(int queueSize) {
        this.queueSize = queueSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public void setKeepAliveTime(long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

}
